package com.roast.pages;

import com.roast.domain.*;
import com.roast.util.ArrayListUtil;
import com.roast.services.my.MyServicesFacade;
import com.roast.services.messges.GetSystemMessageFacade;
import java.util.ArrayList;
import java.util.List;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionAttribute;
import org.apache.tapestry5.ioc.annotations.InjectService;

public class CreateEvent {

    @InjectService("MyServicesFacade")
    MyServicesFacade myServices;

    @InjectService("GetSystemMessageFacade")
    GetSystemMessageFacade getSystemMessage;

    @SessionAttribute
    boolean loginSA;

    @SessionAttribute
    UserData userSA;

    @Persist
    Event event;

    //Page Template
    @Property
    String eventType;

    @Property
    String foreword;

    @Property
    String emailStar;

    @Property
    String starNicknameEmail = "     ";

    @Property
    String emailParticipant;

    @Property
    String participantNicknameEmail = "      ";

    @Property
    String systemMessage = "     ";

    //PatrticipantsTable
    @Property
    UserData userData;

    public List<UserData> getParticipants() {
        if (event == null) {
            event = new Event();
        }

        List<UserData> list = event.getParticipantsList();
        if (list == null) {
            ArrayList<UserData> participantsList = new ArrayList<>();
            event.setParticipantsList(participantsList);
            return event.getParticipantsList();
        } else {
            return list;
        }
    }

    void onActionFromRemove(long userId) {
        ArrayListUtil.removeParticipantByID(userId, event.getParticipantsList());
        participantNicknameEmail = "";
    }

    //Buttons
    private boolean findStar;

    void onSelectedFromFindStar() {
        findStar = true;
    }

    private boolean addParticipant;

    void onSelectedFromAddParticipant() {
        addParticipant = true;
    }

    private boolean create;

    void onSelectedFromCreate() {
        create = true;
    }

    private boolean reset;

    void onSelectedFromReset() {
        reset = true;
    }

    Object onSuccess() {
        if (findStar) {
            return findStar();
        }

        if (addParticipant) {
            return addParticipant();
        }

        if (create) {
            return create();
        }

        if (reset) {
            return reset();
        }

        return null;
    }

    private Object findStar() {
        event.setMaster(userSA);

        if (userSA.getEmail().equals(emailStar)) {
            starNicknameEmail = "Master can't be star!";
            return null;
        }

        UserData star = new UserData();
        star.setEmail(emailStar);

        int signal = myServices.find(star);

        if (signal == 10300) {
            starNicknameEmail = "Star [Nickname: " + star.getNickname() + " | Email: " + star.getEmail() + "]";
            event.setStar(star);
        } else {
            starNicknameEmail = "User doesn't exist!";
        }
        return null;
    }

    private Object addParticipant() {
        String check = starNicknameEmail.substring(0, 4);
        if (!check.equals("Star")) {
            participantNicknameEmail = "First find star!";
            return null;
        }
        if (userSA.getEmail().equals(emailParticipant)) {
            participantNicknameEmail = "Master can't be participant!";
            return null;
        }
        if (event.getStar().getEmail().equals(emailParticipant)) {
            participantNicknameEmail = "Star can't be participant!";
            return null;
        }

        UserData participant = new UserData();
        participant.setEmail(emailParticipant);

        int signal = myServices.find(participant);
        if (signal == 10300) {
            //participantNicknameEmail = "Participant [Nickname: " + participant.getNickname() + " | Email: " + participant.getEmail() + "]";
            if (!ArrayListUtil.isParticipant(event.getParticipantsList(), participant)) {
                event.getParticipantsList().add(participant);
            } else {
                participantNicknameEmail = "Participant [Nickname: " + participant.getNickname() + " | Email: " + participant.getEmail() + "] is already in.";
            }
        } else {
            participantNicknameEmail = "User doesn't exist!";
        }

        return null;
    }

    private Object create() {
        if (eventType.equals("private")) {
            event.setIsPrivate(true);
        } else {
            event.setIsPrivate(false);
        }

        Text eventForeword = new Text();
        eventForeword.setText(foreword);
        event.setForeword(eventForeword);

        List<UserData> list = event.getParticipantsList();
        if (list.isEmpty()) {
            systemMessage = "Add participants!";
            return null;
        } else {
            int signal = myServices.create(event);
            systemMessage = getSystemMessage.create(signal);

            if (signal == 10500) {
                return reset();
            }
            return null;
        }
    }

    private Object reset() {
        event = null;
        eventType = null;
        foreword = null;
        starNicknameEmail = "     ";
        participantNicknameEmail = "      ";

        return null;
    }

    //AJAX
    Object[] array;

    void onActivate(Object[] array) {
        this.array = array;

        String s = (String) array[0];
        this.eventType = s;

        s = (String) array[1];
        this.foreword = s;

        s = (String) array[2];
        this.starNicknameEmail = s;

        s = (String) array[3];
        this.participantNicknameEmail = s;

        s = (String) array[4];
        this.systemMessage = s;
    }

    Object[] onPassivate() {
        array = new Object[]{eventType, foreword, starNicknameEmail, participantNicknameEmail, systemMessage};
        return array;
    }

    Object onActivate() {
        if (!loginSA) {
            return Index.class;
        } else {
            return null;
        }
    }
}
